<?php

namespace app\admin\controller\team;
use app\admin\model\shopro\commission\Level;
use app\admin\model\User;
use app\common\controller\Backend;
class Team extends Backend
{

    protected $noNeedRight=['*'];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\User;
        //$this->view->assign("statusList", $this->model->getStatusList());
    }


    public function teams()
    {
        if(request()->isAjax()){

            try{


                $field = ['id username nickname order_switch money commission score tokens total_consume createtime prevtime parent_user_id lv.name scg.child_order_money_all scg.child_order_money_1 scg.child_user_count_1 scg.child_user_count_all scg.total_income'];
                $data=$this->model
                    //->join('user up','up.id=u.parent_user_id','left')
                    //->join('shopro_commission_agent scg','scg.user_id=user.id','left')
                        ->with(['agentInfo'])
                    //->join('shopro_commission_level lv','lv.level=agent.level')
                    //->where(['parent_user_id'=>0])
                    //->field($field)
                    ->select();


                $tree = [];

                foreach ($data as $item){
                    $level = Level::where('level',$item['agent_info']['level'])->value('name');
                    $text = "用户名：{$item['username']}，昵称：{$item['nickname']}，个人充值：{$item['total_consume']},个人盈利：{$item['agent_info']['total_income']},团队业绩：{$item['agent_info']['child_order_money_all']}，直推人数:{$item['agent_info']['child_agent_count_1']}，供应商等级:{$level}";
                    if ($item['order_switch'] == 1) {
                        $text = "<span style='color:red;'>用户名：{$item['username']}，昵称：{$item['nickname']}，个人充值：{$item['total_consume']},个人盈利：{$item['agent_info']['total_income']},团队业绩：{$item['agent_info']['child_order_money_all']}，直推人数:{$item['agent_info']['child_agent_count_1']}，供应商等级:{$level}</span>";
                    }
                    $tree[] = [
                        'id' => $item['id'],
                        'parent' => $item['parent_user_id'] ?: '#',
//                        'text' => "用户名：{$item['username']}，昵称：{$item['nickname']}，个人充值：{$item['total_consume']},个人盈利：{$item['agent_info']['total_income']},团队业绩：{$item['agent_info']['child_order_money_all']}，直推人数:{$item['agent_info']['child_agent_count_1']}，今日业绩:{$item['agent_info']['today_order_money']}，昨日业绩:{$item['agent_info']['yesday_order_money']}，",
                        'text' => $text,
                        //'text' => "用户名：{$item['username']}，昵称：{$item['username']} 直推人数:{$item['agent_info']['child_order_money_0']}"
                        // 其他需要的字段
                        'username' => $item['username']
                    ];

                }
                return json($tree);
            }catch (\Exception $e){
                return json($e->getMessage());
            }


        }

        return $this->fetch();

    }

    public function sou()
    {
        $prams=request()->only(['username','nickname']);

        if($prams['username']=='' && $prams['nickname']==''){
            $this->error('请至少输入一个条件');
        }

        $where=[];
        if($prams['username']!==''){
            $where['username']=$prams['username'];
        }
        if($prams['nickname']!==''){
            $where['nickname']=$prams['nickname'];
        }

        $user=$this->model->where($where)->find();

        if($user){
            $this->success('ok','',$user['id']);
        }else{
            $this->error('没有该用户');
        }




    }

}